#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Docker镜像加速器有效性检测工具主程序
"""

import argparse
import time
import sys
import os

# 添加项目根目录到Python路径
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from docker_auto.mirrors.detector import MirrorDetector
from docker_auto.mirrors.config import DEFAULT_MIRRORS

def run_cli_test():
    """
    运行命令行检测
    """
    print("Docker镜像加速器有效性检测工具")
    print("=" * 40)
    
    # 创建检测器实例
    detector = MirrorDetector()
    
    print(f"开始检测 {len(DEFAULT_MIRRORS)} 个镜像加速器...")
    print()
    
    # 执行检测
    start_time = time.time()
    results = detector.test_all_mirrors()
    end_time = time.time()
    
    # 显示结果
    print("检测结果:")
    print("-" * 40)
    for result in results:
        status = "✓ 有效" if result["is_valid"] else "✗ 无效"
        response_time = f"{result['response_time']:.3f}s" if result["response_time"] else "N/A"
        
        print(f"URL: {result['mirror_url']}")
        print(f"状态: {status}")
        print(f"响应时间: {response_time}")
        
        if result["error_message"]:
            print(f"错误信息: {result['error_message']}")
        print("-" * 40)
    
    print(f"检测完成，总耗时: {end_time - start_time:.3f}秒")

def main():
    """
    主函数
    """
    parser = argparse.ArgumentParser(description="Docker镜像加速器有效性检测工具")
    parser.add_argument(
        "--mode", 
        choices=["cli", "web"], 
        default="cli", 
        help="运行模式: cli(命令行模式) 或 web(Web服务器模式)"
    )
    
    args = parser.parse_args()
    
    if args.mode == "cli":
        run_cli_test()
    elif args.mode == "web":
        # 启动Web服务器
        from docker_auto.web.server import main as start_server
        start_server()

if __name__ == "__main__":
    main()